package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.NewSign2Bean;
import com.jianyun.jyzs.bean.NewSignRecodeBean;

import java.util.List;

/**
 * Created by yangshijun on 2018/10/11.
 */

public class NewSign2RecordAdapter extends RecyclerView.Adapter<NewSign2RecordAdapter.TextHolder> {

    private Context mContext;
    private List<NewSign2Bean.SignDetail> mList;
    private static final int TITLE_TYPE = 1;
    private static final int CONTENT_TYPE = 2;
    private NewSign2RecordAdapter.OnItemClickListener onItemClick;


    public NewSign2RecordAdapter(Context context, List<NewSign2Bean.SignDetail> signDetail) {
        mContext = context;
        mList = signDetail;
    }

    public void setDataList(List<NewSign2Bean.SignDetail> signDetail) {
        this.mList = signDetail;
        Log.i("test", "记录详情：" + signDetail.toString());
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TextHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        switch (viewType) {
            case TITLE_TYPE:
                View itemView2 = LayoutInflater.from(mContext)
                        .inflate(R.layout.item_newsign_record1, parent, false);
                return new TextHolder(itemView2);

            case CONTENT_TYPE:
                View itemView = LayoutInflater.from(mContext)
                        .inflate(R.layout.item_newsign_record, parent, false);
                return new TextHolder(itemView);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(@NonNull TextHolder holder, final int position) {
        if (position == 0) {
            holder.tvTitle.setText("打卡记录");
        } else {
            String workType = mList.get(position - 1).getWorkType();
            String signStatusCode = mList.get(position - 1).getSignStatusCode();
            String signTime = mList.get(position - 1).getSignTime();
            String signAddressStatusCode = mList.get(position - 1).getSignAddressStatusCode();
            String signAddress = mList.get(position - 1).getSignAddress();
            boolean isShowSignStatusCode = mList.get(position - 1).getIsShowSignStatusCode();
            if (null != mList.get(position - 1).getWorkTime()) {
                holder.tvTime.setText("" + mList.get(position - 1).getWorkTime());
            }
            if (null != workType) {
                if ("1".equals(workType)) {
                    holder.workType.setText("上班");
                    holder.ll_1.setVisibility(View.INVISIBLE);
                    holder.ll_2.setVisibility(View.VISIBLE);
                } else {
                    holder.workType.setText("下班");
                    holder.ll_2.setVisibility(View.INVISIBLE);
                    holder.ll_1.setVisibility(View.VISIBLE);
                }
            }
            if (null != signStatusCode) {
                switch (signStatusCode) {
                    case "1":
                        //正常
                        holder.workType.setTextColor(mContext.getResources().getColor(R.color.bacck333));
                        if (isShowSignStatusCode){
                            holder.signTypeTime.setText("正常" + "（" + signTime + "）");
                        }else {
                            holder.signTypeTime.setText( signTime );
                        }
                        holder.signTypeTime.setTextColor(mContext.getResources().getColor(R.color.bacck333));
                        if (null != signAddress) {
                            holder.signLocation.setText("" + signAddress);
                        }
                        holder.llApply.setVisibility(View.INVISIBLE);
                        break;
                    case "2":
                        //迟到
                        holder.workType.setTextColor(mContext.getResources().getColor(R.color.bacck333));
                        if (isShowSignStatusCode){
                            holder.signTypeTime.setText("迟到" + "（" + signTime + "）");
                        }else {
                            holder.signTypeTime.setText( signTime );

                        }
                        holder.signTypeTime.setSelected(true);
                        if (null != signAddress) {
                            holder.signLocation.setText("" + signAddress);
                        }
                        holder.llApply.setVisibility(View.INVISIBLE);
                        break;
                    case "3":
                        //早退
                        holder.workType.setTextColor(mContext.getResources().getColor(R.color.bacck333));
                        if (isShowSignStatusCode){
                            holder.signTypeTime.setText("早退" + "（" + signTime + "）");
                        }else {
                            holder.signTypeTime.setText( signTime );

                        }
                        holder.signTypeTime.setSelected(true);
                        if (null != signAddress) {
                            holder.signLocation.setText("" + signAddress);
                        }
                        holder.llApply.setVisibility(View.INVISIBLE);
                        break;
                    case "4":
                        //未打卡
                        holder.workType.setTextColor(mContext.getResources().getColor(R.color.bacck333));
                        holder.signTypeTime.setText("未打卡");
                        holder.signTypeTime.setSelected(true);
                        if (null != signAddress) {
                            holder.signLocation.setText("" + signAddress);
                        }
                        holder.llApply.setVisibility(View.INVISIBLE);
                        break;
                    case "5":
                        //未到打卡时间
                        holder.workType.setTextColor(mContext.getResources().getColor(R.color.bacck333));
                        holder.signTypeTime.setText("");
                        holder.signTypeTime.setSelected(true);
                        if (null != signAddress) {
                            holder.signLocation.setText("" + signAddress);
                        }
                        holder.llApply.setVisibility(View.INVISIBLE);
                        break;

                }
            }
            if (null != signAddressStatusCode) {
                if ("1".equals(signAddressStatusCode)) {
                    holder.signLocation.setTextColor(mContext.getResources().getColor(R.color.bacck333));
                } else if ("2".equals(signAddressStatusCode)) {
                    holder.signLocation.setTextColor(mContext.getResources().getColor(R.color.ff33));
                }

            }
            try {
                if (onItemClick != null) {
//                    holder.llApply.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            NewSignRecodeBean.SignDetail signDetail = mList.get(position);
//                            onItemClick.onItemClick(position, v, signDetail);
//                        }
//                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


        }

    }

    @Override
    public int getItemViewType(int position) {
        //当position为0的时候，那个我们需要显示的是头部
        if (position == 0) {
            //==0的情况
            return TITLE_TYPE;
        } else {
            //！==0的时候就返回全是相同的Item
            return CONTENT_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }


    public void setOnItemClickListener(NewSign2RecordAdapter.OnItemClickListener onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View view, NewSignRecodeBean.SignDetail listdata);
    }

    class TextHolder extends RecyclerView.ViewHolder {

        TextView tvTime;
        LinearLayout ll_1;
        LinearLayout ll_2;
        TextView workType;
        TextView signTypeTime;
        TextView signLocation;
        LinearLayout llApply;
        TextView tvTitle;

        TextHolder(View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_time);
            ll_1 = itemView.findViewById(R.id.ll_1);
            ll_2 = itemView.findViewById(R.id.ll_2);
            workType = itemView.findViewById(R.id.work_type);
            signTypeTime = itemView.findViewById(R.id.sign_type_time);
            signLocation = itemView.findViewById(R.id.sign_location);
            llApply = itemView.findViewById(R.id.ll_apply);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }
}

